#include "HttpRequest.h"
#include "HttpRequesterCURL.h"
#include "PlayFabResultHandler.h"
#include "PlayFabSettings.h"
#include "PlayFab<%- api.name %>API.h"
#include <string>

using namespace PlayFab;
using namespace PlayFab::<%- api.name %>Models;

PlayFab<%- api.name %>API::PlayFab<%- api.name %>API() {}

<% for(var i in api.calls) { var apiCall = api.calls[i];
%>void PlayFab<%- api.name %>API::<%- apiCall.name %>(
<% if (hasRequest(apiCall, api)) { %>    <%- apiCall.request %>& request,
<% } %>    ProcessApiCallback<<%- apiCall.result %>> callback,
    ErrorCallback errorCallback,
    void* userData
)
{
<%- getRequestActions("    ", apiCall)
%>    HttpRequest* httpRequest = new HttpRequest("POST", <%- getUrlAccessor(apiCall) %>);
    httpRequest->SetHeader("Content-Type", "application/json");
    httpRequest->SetHeader("X-PlayFabSDK", PlayFabSettings::versionString);
<%- getAuthParams("    ", apiCall) %>
    if (callback != nullptr)
        httpRequest->SetResultCallback(SharedVoidPointer(new ProcessApiCallback<<%- apiCall.result %>>(callback)));
    httpRequest->SetErrorCallback(errorCallback);
    httpRequest->SetUserData(userData);

    <% if (hasRequest(apiCall, api)) { %>httpRequest->SetBody(request.toJSONString());<% } else { %>httpRequest->SetBody("{}");<% } %>
    httpRequest->CompressBody();

    PlayFabSettings::httpRequester->AddRequest(httpRequest, On<%- apiCall.name %>Result, userData);
}

void PlayFab<%- api.name %>API::On<%- apiCall.name %>Result(int httpStatus, HttpRequest* request, void* userData)
{
    <%- apiCall.result %> outResult;
    PlayFabError errorResult;

    if (PlayFabRequestHandler::DecodeRequest(httpStatus, request, userData, outResult, errorResult))
    {
<%- getResultActions("        ", apiCall)
%>        if (request->GetResultCallback() != nullptr)
        {
            (*static_cast<ProcessApiCallback<<%- apiCall.result %>> *>(request->GetResultCallback().get()))(outResult, request->GetUserData());
        }
    }
    else
    {
        if (PlayFabSettings::globalErrorHandler != nullptr)
            PlayFabSettings::globalErrorHandler(errorResult, request->GetUserData());
        if (request->GetErrorCallback() != nullptr)
            request->GetErrorCallback()(errorResult, request->GetUserData());
    }

    delete request;
}

<% } %><% if (hasClientOptions) {
%>bool PlayFabClientAPI::IsClientLoggedIn()
{
    return !PlayFabSettings::clientSessionTicket.empty();
}

void PlayFabClientAPI::MultiStepClientLogin(bool needsAttribution)
{
    if (needsAttribution && !PlayFabSettings::disableAdvertising && PlayFabSettings::advertisingIdType.length() > 0 && PlayFabSettings::advertisingIdValue.length() > 0)
    {
        AttributeInstallRequest request;
        if (PlayFabSettings::advertisingIdType == PlayFabSettings::AD_TYPE_IDFA)
            request.Idfa = PlayFabSettings::advertisingIdValue;
        else if (PlayFabSettings::advertisingIdType == PlayFabSettings::AD_TYPE_ANDROID_ID)
            request.Adid = PlayFabSettings::advertisingIdValue;
        else
            return;
        AttributeInstall(request, nullptr, nullptr);
    }
}<% } %>
